<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Api;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Request;
use Hyperf\View\RenderInterface;
use Illuminate\Support\Facades\Hash;
class ProductController
{


     public function product_info(){
            $brand = [];
            $category =[];
            $standards =[];
            $pack_method =[];
            $info = DB::table('product')->select('category','brand','standards','pack_method')->get();
            foreach ($info as $key=>$value){
                if(!in_array($value->brand,$brand)) {
                    $brand[] = $value->brand;
                }
                if(!in_array($value->category,$category)) {
                    $category[] = $value->category;
                }
                if(!in_array($value->standards,$standards)) {
                    $standards[] = $value->standards;
                }
                if(!in_array($value->pack_method,$pack_method)) {
                    $pack_method[] = $value->pack_method;
                }
            }
           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data']['brand'] = $brand;
           $res['data']['category'] = $category;
           $res['data']['standards'] = $standards;
           $res['data']['pack_method'] = $pack_method;

           return json_encode($res,1);
     }

     /*
      * brand
      * category
      * standards
      * pack_method
      * */


     public function product_list(Request $request){
           // echo $_SERVER['SERVER_NAME'];

              if(!empty($request->brand)&&!empty($request->category)&&!empty($request->standards)&&!empty($request->pack_method)){
                  $product = DB::table('product')
                      ->select('id', 'category', 'brand', 'standards', 'pack_method', 'pay_position', 'company_name', 'img_id')
                      ->where('brand','like','%'.$request->brand.'%')
                      ->where('category','like','%'.$request->category.'%')
                      ->where('standards','like','%'.$request->standards.'%')
                      ->where('pack_method','like','%'.$request->pack_method.'%')
                      ->get();
              }else {
                  $product = DB::table('product')
                      ->select('id', 'category', 'brand', 'standards', 'pack_method', 'pay_position', 'company_name', 'img_id')
                      ->get();
              }

            $product = json_decode($product,1);
            foreach ($product as $key=>$value){
            $product[$key]['product_name'] = $value['brand'].$value['category'].$value['standards'];
            $image_string = $value['img_id'];
            $img_id = explode(',', $image_string);
            $images = DB::table('product_images')->select('name')->whereIn('id',$img_id)->get();
            //$images = json_decode($images,1);
            $pic = [];
            foreach ($images as $k=>$v){
                $pic[] = $_SESSION['network'].$_SERVER['SERVER_NAME'].'/product/'.$v->name;
            }

            $product[$key]['pic'] = $pic;
            }

           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data'] = $product;
            return json_encode($res,1);
      }


      /*
       * produce_id
       *
       *
       * */


      public function product_detail(Request $request){

           $product = DB::table('product')
               ->select('*')
               ->where('id',$request->produce_id)
               ->first();
           $product = (array)$product;
           $product['product_name'] = $product['brand'].$product['category'].$product['standards'];
           $img_id = explode(',', $product['img_id']);
           $images = DB::table('product_images')->select('name')->whereIn('id',$img_id)->get();
           $pic = [];
            foreach ($images as $key=>$value){
                $pic[] = $_SESSION['network'].$_SERVER['SERVER_NAME'].'/product/'.$value->name;
            }
           $product['arg']['pic'] = $pic[0];
           $product['detail']['pic'] = $pic;
           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data'] = $product;
           return json_encode($res,1);
      }


      /*
       * week
       *
       * months
       *
       * product_id
       *
       * */

      public function price(Request $request){

          if($request->week == 1&& $request->ticket == 1 ){

                $now_time = strtotime( "today");
                $now_date = date('Y-m-d H:i:s',$now_time);
                $before_time = strtotime("-1 week");
                $before_date = date('Y-m-d H:i:s',$before_time);
                $product_price = DB::table('product_price')
                    ->where('date','>',$before_date)
                    ->where('date','<',$now_date)
                    ->where('product_id',$request->product_id)
                    ->get();
               $categories = [];
               $series = [];
               $series[0]['name'] = '最高';
               $series[1]['name'] = '最低';
               $series[0]['color'] = '#479FF8';
               $series[1]['color'] = '#83D755';
               foreach ($product_price as $key=>$value){
                        $categories[] = strtotime($value->date);
                        $series[0]['data'][] = $value->top_price;
                        $series[1]['data'][] = $value->bottom_price;
               }

               $yAxis['max'] = max($series[0]['data']);
               $yAxis['min'] = min($series[1]['data']);
          }else if($request->week == 0&& $request->ticket == 0 ){

                $now_time = strtotime( "today");
                $now_date = date('Y-m-d H:i:s',$now_time);
                $before_time = strtotime("-1 month");
                $before_date = date('Y-m-d H:i:s',$before_time);
                $product_price = DB::table('product_price')
                    ->where('date','>',$before_date)
                    ->where('date','<=',$now_date)
                    ->where('product_id',$request->product_id)
                    ->get();
               $categories = [];
               $series = [];
               $series[0]['name'] = '最高';
               $series[1]['name'] = '最低';
               $series[0]['color'] = '#479FF8';
               $series[1]['color'] = '#83D755';
               foreach ($product_price as $key=>$value){
                        $categories[] = strtotime($value->date);
                        $series[0]['data'][] = $value->top_price + $value->cards;
                        $series[1]['data'][] = $value->bottom_price  + $value->cards;
               }

               $yAxis['max'] = max($series[0]['data']);
               $yAxis['min'] = min($series[1]['data']);
          }else if($request->week == 0&& $request->ticket == 1 ){

                $now_time = strtotime( "today");
                $now_date = date('Y-m-d H:i:s',$now_time);
                $before_time = strtotime("-1 month");
                $before_date = date('Y-m-d H:i:s',$before_time);
                $product_price = DB::table('product_price')
                    ->where('date','>',$before_date)
                    ->where('date','<=',$now_date)
                    ->where('product_id',$request->product_id)
                    ->get();
               $categories = [];
               $series = [];
               $series[0]['name'] = '最高';
               $series[1]['name'] = '最低';
               $series[0]['color'] = '#479FF8';
               $series[1]['color'] = '#83D755';
               foreach ($product_price as $key=>$value){
                        $categories[] = strtotime($value->date);
                        $series[0]['data'][] = $value->top_price;
                        $series[1]['data'][] = $value->bottom_price;
               }

               $yAxis['max'] = max($series[0]['data']);
               $yAxis['min'] = min($series[1]['data']);


          }else if($request->week == 1&& $request->ticket == 0 ){

                $now_time = strtotime( "today");
                $now_date = date('Y-m-d H:i:s',$now_time);
                $before_time = strtotime("-1 week");
                $before_date = date('Y-m-d H:i:s',$before_time);
                $product_price = DB::table('product_price')
                    ->where('date','>',$before_date)
                    ->where('date','<=',$now_date)
                    ->where('product_id',$request->product_id)
                    ->get();
               $categories = [];
               $series = [];
               $series[0]['name'] = '最高';
               $series[1]['name'] = '最低';
               $series[0]['color'] = '#479FF8';
               $series[1]['color'] = '#83D755';
               foreach ($product_price as $key=>$value){
                        $categories[] = strtotime($value->date);
                        $series[0]['data'][] = $value->top_price + $value->cards;
                        $series[1]['data'][] = $value->bottom_price + $value->cards;
               }

               $yAxis['max'] = max($series[0]['data']);
               $yAxis['min'] = min($series[1]['data']);
          }

           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data']['categories'] = $categories;
           $res['data']['series'] = $series;
           $res['data']['yAxis'] = $yAxis;

           return json_encode($res,1);
      }


      /*
       * supplyer_id
       * produce_id
       * */
      public function product_arg(Request $request){
          $product = DB::table('product')->select('*')->where('id',$request->produce_id)->first();
           $product = (array)$product;
           $product['product_name'] = $product['brand'].$product['category'].$product['standards'];
           $img_id = explode(',', $product['img_id']);
           $images = DB::table('product_images')->select('name')->whereIn('id',$img_id)->get();
           $pic = [];
            foreach ($images as $key=>$value){
                $pic[] = $_SESSION['network'].$_SERVER['SERVER_NAME'].'/product/'.$value->name;
            }
           $product['pic'] = $pic[0];
           $res['status'] = true;
           $res['code'] = 200;
           $res['msg'] = '';
           $res['data'] = $product;
           return json_encode($res,1);
      }











}




?>